package com.yunche.yunchebusiness.ui.activity.finance

import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.bigkoo.pickerview.listener.OnOptionsSelectListener
import com.bumptech.glide.Glide
import com.yunche.yunchebusiness.R
import com.yunche.yunchebusiness.api.Api
import com.yunche.yunchebusiness.base.BaseActivity
import com.yunche.yunchebusiness.common.CommonDialog
import com.yunche.yunchebusiness.entity.BankListEntity
import com.yunche.yunchebusiness.entity.TenantInfoEntity
import com.yunche.yunchebusiness.isIdCard
import com.yunche.yunchebusiness.mvp.contract.AddTenantContract
import com.yunche.yunchebusiness.mvp.presenter.AddTenantPresenter
import com.yunche.yunchebusiness.network.RequestJson
import com.yunche.yunchebusiness.utils.ToastyUtils
import kotlinx.android.synthetic.main.activity_add_bank.*

class AddBankActivity : BaseActivity(), AddTenantContract.View {
    private val mPresenter by lazy {
        AddTenantPresenter()
    }
    var bankId = 0
    private var tenantEntity: TenantInfoEntity? = null

    private var bankListEntity = listOf<BankListEntity>()

    override fun getBankListSuccess(bankListEntity: List<BankListEntity>) {
        this.bankListEntity = bankListEntity
    }

    override fun submitSuccess() {
        ToastyUtils.showInfo("提交成功")
        finish()
    }

    override fun showLoading() {
        CommonDialog.showLoading(mContext!!, "加载中...")
    }

    override fun hideLoading() {
        CommonDialog.cancle()
    }

    override fun showError(msg: String) {
        ToastyUtils.showError(msg)
    }

    override fun setLayout(): Int = R.layout.activity_add_bank

    override fun initView() {
        mPresenter.attachView(this)
    }

    override fun initData() {
        tenantEntity = intent.extras.getSerializable("tenant") as TenantInfoEntity
        var para = JSONObject()
        mPresenter.getBankList(RequestJson.getRequestBody(para.toJSONString()))
    }

    override fun initEvent() {

        et_account_bank_name.setOnClickListener {
            if (bankListEntity == null) {
                initData()
            } else {
                showBankList()
            }
        }

        btn_add_bank.setOnClickListener {
            submit()
        }


        linlay_bank_list.setOnClickListener {
            if (bankListEntity == null) {
                initData()
            } else {
                showBankList()
            }
        }

    }

    private fun submit() {
        if (bankId == 0) {
            ToastyUtils.show("请选择银行")
            return
        }

        if (et_account.text.toString().isEmpty()) {
            ToastyUtils.show("请输入持卡人姓名")
            return
        }

        if (et_account_bank_num.text.toString().isEmpty()) {
            ToastyUtils.show("请输入卡号")
            return
        } else {
            tenantEntity?.accountList!!.forEach { account ->
                if (et_account_bank_num.text.toString() == account.account) {
                    ToastyUtils.show("卡号已存在，请重新输入")
                    return
                }
            }
        }

        if (et_account_id_card.text.toString().isEmpty() || !et_account_id_card.text.toString().isIdCard()) {
            ToastyUtils.show("请输入正确的身份证号码")
            return
        }


        var accountList = JSONArray()
        tenantEntity?.accountList!!.forEach { account ->
            var accountJson = JSONObject()
            accountJson["account"] = account.account
            accountJson["accountName"] = account.accountName
            accountJson["bank"] = account.bank
            accountJson["accoutCardId"] = account.accoutCardId
            accountList.add(accountJson)
        }

        var accountJson = JSONObject()
        accountJson["account"] = et_account_bank_num.text.toString()
        accountJson["accountName"] = et_account.text.toString()
        accountJson["bank"] = bankId
        accountJson["accoutCardId"] = et_account_id_card.text.toString()

        accountList.add(accountJson)
        var para = JSONObject()
        para["accountList"] = accountList

        mPresenter.submit(RequestJson.getRequestBody(para.toJSONString()), Api.getTenantId())
    }

    private fun showBankList() {
        var bankList = arrayListOf<String>()
        for (bankListEntity in bankListEntity) {
            bankList.add(bankListEntity.name)
        }

        val pvOptions = OptionsPickerBuilder(mContext,
            OnOptionsSelectListener { options1, _, _, _ ->
                et_account_bank_name.setText(bankListEntity[options1].name)
                bankId = bankListEntity[options1].id
                Glide.with(mContext!!).load(bankListEntity[options1].icon).into(iv_bank_icon)
            }).build<String>()
        pvOptions.setPicker(bankList)
        pvOptions.show()
    }
}
